package com.kun.video.ui.control.button;

import com.kun.video.context.ContextService;
import com.kun.video.ui.EventQueue;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.scene.Node;

import java.util.concurrent.CompletableFuture;

/**
 * @author gzc
 */
public class KunButton extends AbstractButton {
    private final EventQueue eventQueue;

    public KunButton(String buttonText) {
        this(buttonText, null, null);
    }

    public KunButton(String buttonText, String tipText) {
        this(buttonText, null, tipText);
    }

    public KunButton(String buttonText, Node svgGraphic, String tipText) {
        super(buttonText, svgGraphic, tipText);
        eventQueue = ContextService.getBean(EventQueue.class);
    }

    @Override
    public ButtonEvent createButtonEvent() {
        EventHandler<ActionEvent> eventHandler = onActionProperty().getValue();
        return () -> {
            if (eventHandler != null) {
                eventHandler.handle(new ActionEvent());
            }
        };
    }

    @Override
    protected CompletableFuture<Void> doHandleAction(ButtonEvent event,
                                                     Runnable successCallback,
                                                     Runnable failureCallback) {
        return eventQueue.submitEvent(event, successCallback, failureCallback);
    }

}
